package com.simon.jkxj.ui.conditioning;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.*;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.simon.jkxj.R;
import java.util.ArrayList;
import android.app.ProgressDialog;
import android.net.Uri;
import android.content.ActivityNotFoundException;
import java.util.List;

public class HealthSelfTestActivity extends AppCompatActivity {
    private static final int REQUEST_CODE_UPLOAD = 1001;
    private String uploadedFileName = "";
    private String[] mainSymptoms = {"头痛", "咳嗽", "发热", "乏力", "腹泻"};
    private String[] otherSymptoms = {"恶心", "呕吐", "流涕", "咽痛", "胸闷"};
    private List<CheckBox> mainSymptomChecks = new ArrayList<>();
    private List<CheckBox> otherSymptomChecks = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health_self_test);

        LinearLayout layoutMainSymptoms = findViewById(R.id.layout_main_symptoms);
        for (String s : mainSymptoms) {
            CheckBox cb = new CheckBox(this);
            cb.setText(s);
            layoutMainSymptoms.addView(cb);
            mainSymptomChecks.add(cb);
        }
        LinearLayout layoutOtherSymptoms = findViewById(R.id.layout_other_symptoms);
        for (String s : otherSymptoms) {
            CheckBox cb = new CheckBox(this);
            cb.setText(s);
            layoutOtherSymptoms.addView(cb);
            otherSymptomChecks.add(cb);
        }

        Button btnUpload = findViewById(R.id.btn_upload_report);
        TextView tvUploadStatus = findViewById(R.id.tv_upload_status);
        btnUpload.setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("application/pdf|image/*");
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            try {
                startActivityForResult(Intent.createChooser(intent, "选择体检报告"), REQUEST_CODE_UPLOAD);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(this, "未找到文件管理器", Toast.LENGTH_SHORT).show();
            }
        });

        SeekBar seekBarPain = findViewById(R.id.seekbar_pain);
        TextView tvPainLevel = findViewById(R.id.tv_pain_level);
        seekBarPain.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tvPainLevel.setText((progress + 1) + "分");
            }
            @Override public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override public void onStopTrackingTouch(SeekBar seekBar) {}
        });

        Spinner spinnerDuration = findViewById(R.id.spinner_duration);
        String[] durations = {"1天内", "1-3天", "3-7天", "1周以上"};
        ArrayAdapter<String> durationAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, durations);
        durationAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerDuration.setAdapter(durationAdapter);

        findViewById(R.id.btn_submit).setOnClickListener(v -> {
            // 1. 显示加载对话框
            ProgressDialog dialog = new ProgressDialog(this);
            dialog.setMessage("AI正在分析您的健康状况...");
            dialog.setCancelable(false);
            dialog.show();

            // 2. 收集数据并简单打分
            int score = 95;
            int pain = seekBarPain.getProgress() + 1;
            if (pain > 6) score -= 10;
            int mainCount = 0;
            for (CheckBox cb : mainSymptomChecks) if (cb.isChecked()) mainCount++;
            if (mainCount > 2) score -= 5;
            int otherCount = 0;
            for (CheckBox cb : otherSymptomChecks) if (cb.isChecked()) otherCount++;
            if (otherCount > 2) score -= 5;
            if (uploadedFileName.isEmpty()) score -= 2;
            if (score < 70) score = 70 + (int)(Math.random()*5);
            if (score > 99) score = 99;

            // 3. 2秒后跳转结果页
            int finalScore = score;
            new Handler().postDelayed(() -> {
                dialog.dismiss();
                Intent intent = new Intent(this, HealthSelfTestResultActivity.class);
                intent.putExtra("score", finalScore);
                startActivity(intent);
            }, 2000);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_UPLOAD && resultCode == RESULT_OK && data != null) {
            Uri uri = data.getData();
            if (uri != null) {
                uploadedFileName = uri.getLastPathSegment();
                TextView tvUploadStatus = findViewById(R.id.tv_upload_status);
                tvUploadStatus.setText("已上传: " + uploadedFileName);
            }
        }
    }
} 